/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.winner.lps.modules.app.web;

import com.winner.lps.common.config.Global;
import com.winner.lps.common.persistence.Page;
import com.winner.lps.common.utils.StringUtils;
import com.winner.lps.common.web.BaseController;
import com.winner.lps.modules.app.entity.AppPersonnel;
import com.winner.lps.modules.app.service.AppPersonnelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 保障人员管理Controller
 *
 * @author 程少威
 * @version 2017-04-09
 */
@Controller
@RequestMapping(value = "${adminPath}/app/appPersonnel")
public class AppPersonnelController extends BaseController {

    @Autowired
    private AppPersonnelService appPersonnelService;

    @ModelAttribute
    public AppPersonnel get(@RequestParam(required = false) String id) {
        AppPersonnel entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = appPersonnelService.get(id);
        }
        if (entity == null) {
            entity = new AppPersonnel();
        }
        return entity;
    }

    @RequiresPermissions("app:appPersonnel:view")
    @RequestMapping(value = {"list", ""})
    public String list(AppPersonnel appPersonnel, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<AppPersonnel> page = appPersonnelService.findPage(new Page<AppPersonnel>(request, response), appPersonnel);
        model.addAttribute("page", page);
        return "modules/app/appPersonnelList";
    }

    @RequiresPermissions("app:appPersonnel:view")
    @RequestMapping(value = "form")
    public String form(AppPersonnel appPersonnel, Model model) {
        model.addAttribute("appPersonnel", appPersonnel);
        return "modules/app/appPersonnelForm";
    }

    @RequiresPermissions("app:appPersonnel:edit")
    @RequestMapping(value = "save")
    public String save(AppPersonnel appPersonnel, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, appPersonnel)) {
            return form(appPersonnel, model);
        }
        appPersonnelService.save(appPersonnel);
        addMessage(redirectAttributes, "保存保障人员管理成功");
        return "redirect:" + Global.getAdminPath() + "/app/appPersonnel/?repage";
    }

    @RequiresPermissions("app:appPersonnel:edit")
    @RequestMapping(value = "delete")
    public String delete(AppPersonnel appPersonnel, RedirectAttributes redirectAttributes) {
        appPersonnelService.delete(appPersonnel);
        addMessage(redirectAttributes, "删除保障人员管理成功");
        return "redirect:" + Global.getAdminPath() + "/app/appPersonnel/?repage";
    }

}